package tq.com.tqteacher.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.security.PublicKey;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQNewPublish;

public class TQViewPublishDetailInfo extends FrameLayout implements View.OnClickListener {
    private ConstraintLayout viewer;
    private ConstraintLayout leaverMsg;
    private TextView mTitleView;
    private TextView mDescView;
    private TextView line2;

    private TextView liuyan_count;
    private ImageView liuyan_header_icon;
    private TextView liuyan_user_name;
    private TextView liuyan_content;

    private TextView viewer_count;

    public TQViewPublishDetailInfo(@NonNull Context context, @Nullable AttributeSet attrs){
        super(context, attrs);
        initUI();
    }

    private void initUI(){
        LayoutInflater.from(getContext()).inflate(R.layout.vh_publish_card_layout, this, true);

        viewer = findViewById(R.id.viewer);
        leaverMsg = findViewById(R.id.liuyan);

        mTitleView = findViewById(R.id.title);
        mDescView =  findViewById(R.id.content1);
        line2 = findViewById(R.id.line2);

        viewer.setOnClickListener(this);
        leaverMsg.setOnClickListener(this);
        this.setOnClickListener(this);

        liuyan_count = findViewById(R.id.liuyan_count);
        viewer_count = findViewById(R.id.viewer_count);
        initLeaveMsg();
    }

    @Override
    public void onClick(View v) {
        if(mOnClickListener == null) return;

        switch (v.getId()){
            case R.id.viewer:{
                mOnClickListener.showViewer();
            }break;

            case R.id.liuyan:{
                mOnClickListener.showLeverMsg();
            }break;
            default:{
                mOnClickListener.showDetail();
            }
        }
    }

    public void bindData(TQNewPublish pPublish){
        if(pPublish == null) return;

        mTitleView.setText(pPublish.getName());
        mDescView.setText(pPublish.getDesc());

        boolean showLine =false;
        if(pPublish.getCommentOverInfoModel() != null && pPublish.getCommentOverInfoModel().getCount() > 0
                && pPublish.getCommentOverInfoModel().getCommentInfoModel() != null){
            leaverMsg.setVisibility(VISIBLE);
            liuyan_count.setText(String.format("留言（%d）", pPublish.getCommentOverInfoModel().getCount()));
            showLine = true;


            RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.tx3).circleCrop();
            String url = TQPictureUpload.downloadPath(pPublish.getCommentOverInfoModel().getCommentInfoModel().getUserInfo().getPic());
            Glide.with(this).load(url)
                    .apply(lOptions)
                    .into(liuyan_header_icon);

            liuyan_user_name.setText(pPublish.getCommentOverInfoModel().getCommentInfoModel().getUserInfo().getName());
            if(pPublish.getCommentOverInfoModel().getCommentInfoModel().getType() == TQSystemDefine.LeaveMsgType_text){
                liuyan_content.setText(pPublish.getCommentOverInfoModel().getCommentInfoModel().getContent());
            }else if(pPublish.getCommentOverInfoModel().getCommentInfoModel().getType() == TQSystemDefine.LeaveMsgType_image){
                liuyan_content.setText("[图片信息]");
            }else {
                liuyan_content.setText("[视频信息]");
            }

        }else{
            leaverMsg.setVisibility(GONE);
        }

        if(pPublish.getLookerOverInfo() != null && pPublish.getLookerOverInfo().getCount() > 0
                && pPublish.getLookerOverInfo().getPics().size() > 0){
            viewer.setVisibility(VISIBLE);
            viewer_count.setText(String.format("查看（%d）", pPublish.getLookerOverInfo().getCount()));
            showLine = true;
        }else{
            viewer.setVisibility(GONE);
        }

        line2.setVisibility(showLine ? VISIBLE : GONE);
    }

    private void initLeaveMsg(){
        View lItem = null;
        for (int i  =0; i < leaverMsg.getChildCount(); i++){
            View lView = leaverMsg.getChildAt(i);
            if(lView.getId() == R.id.liuyan_title){
                continue;
            }

            lItem = lView;
        }

        if(lItem == null){
            GradientDrawable lDrawable = new GradientDrawable();
            lDrawable.setColor(Color.parseColor("#f8f8f8"));
            lDrawable.setCornerRadius(10 * (int)getResources().getDisplayMetrics().density);

            lItem = LayoutInflater.from(getContext()).inflate(R.layout.view_leavemsg0_layout, this, false);
            lItem.setBackground(lDrawable);

            ConstraintLayout.LayoutParams lParams = new ConstraintLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            lParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
            lParams.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
            lParams.leftMargin = (int) (20 * getResources().getDisplayMetrics().density);
            lParams.topToBottom = R.id.liuyan_title;
            leaverMsg.addView(lItem, lParams);

            liuyan_header_icon = lItem.findViewById(R.id.header_icon);
            liuyan_user_name = lItem.findViewById(R.id.user_name);
            liuyan_content = lItem.findViewById(R.id.content);
        }



    }
    public interface IOnClickListener{
        void showViewer();
        void showLeverMsg();

        void showDetail();
    }

    private IOnClickListener mOnClickListener;
    public void setOnClickListener(IOnClickListener pOnClickListener){
        mOnClickListener = pOnClickListener;
    }
}
